<?php
/**
 * Role Based Access Control
 * 
 * $Rev: 8 $
 * $Author: thejahweh $
 * $Date: 2010-09-14 07:40:11 +0000 (Tue, 14 Sep 2010) $
 * $Id: rbac.php 8 2010-09-14 07:40:11Z thejahweh $
 * $URL: http://allone.googlecode.com/svn/trunk/plugins/rbac/rbac.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

defined('OUTPUT') or die();
global $user;

//$_SESSION['user_id'] = 1;
//$_SESSION['user_ip'] = '127.0.0.1';
//$_POST['a']			= 'login';
//$_POST['name']		= 'testUser';
//$_POST['password']	= 'abcdefg';
//$_COOKIE['login']		=	'dGVzdFVzZXItfC1kMDVlZmZiYTY4MDJlZWM3ZDJhNmI3YjE4ZjU1ZTRlMWQ5Yzk'.
//							'zYjE2ODA0NzM4NWQyNzZkNGE3MTlhNmNhNTAxZGYyNjBkMDA4ZWIzMDljMGQzZj'.
//							'VmOGJmOWQwMDg3NjFiOTEzOGJkZTYyNDEzZmNlZDAyYzYwYTUwMTIwMzU4Mg==';

if (isset($_GET['createhash']) === true or isset($_GET['createhashe']) === true) {
	$hashTime = new timeStamp('HashTime');
	if (isset($_GET['createhash']) === true) {
		u::o('hash:         <span class="var">'.user::hash($_GET['createhash']).'</span>', 'user:createHash', 3);
	} else {
		user::hash($_GET['createhashe'], true);
	}
	$hashTime->end();
}

if (isset($_POST['logout'])) {
	$user = new user();
	$user->removeSession();
	$user->removeCookie();
} elseif (isset($_POST['a']) and $_POST['a'] === 'login' and isset($_POST['name']) and isset($_POST['password'])) {
	$user = new user();
	$user->getByLogin($_POST['name'], $_POST['password']);
}
/**
 * TODO Remove Testuserlogin
 */
elseif (isset($_GET['a']) and $_GET['a'] === 'login' and isset($_GET['name']) and isset($_GET['password'])) {
	$user = new user();
	$user->getByLogin($_GET['name'], $_GET['password']);
} elseif (isset($_SESSION['user_id'], $_SERVER['REMOTE_ADDR'], $_SESSION['user_ip']) and $_SESSION['user_ip'] === $_SERVER['REMOTE_ADDR']) {
	$user = new user();
	$user->getUser($_SESSION['user_id']);
} elseif (isset($_COOKIE[user::$cookieName])) {
	$unp = explode(user::$cookieDelimiter, base64_decode($_COOKIE[user::$cookieName]));
	$user = new user();
	$user->getUserByLogin($unp[0], $unp[1], true);
}

if (isset($user) and $user->isLoaded) {
	$user->setSession();
	/**
	 * TODO Editor Mode!!
	 */
	if (isset($_GET['editMode']) and $_GET['editMode'] === 'on') {
		$user->editMode = true;
	}
//	$user->setCookie();
} else {
	$user = new user();
	/**
	 * TODO Set Default-User from Config
	 */
	$user->getUser(3);
	$user->isLoaded = false;
}
u::o('Current User: <span class="var">'.$user->name.'</span> (ID='.$user->id.')', 'pluginLoader:loadPlugins', 3);

?>